package com.legrand.www.module.worker.feign;

import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * @author xiah
 * @date 2018/5/11 16:42
 */
@Configuration
public class UserFeignServerConfiguration {
       @Bean
       @ConditionalOnMissingBean
       public Logger.Level loggerLevel() {
              return Logger.Level.BASIC;
       }

       @Bean
       public RequestInterceptor headerInterceptor() {
              return new RequestInterceptor() {
                     @Override
                     public void apply(RequestTemplate requestTemplate) {
                            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
                                    .getRequestAttributes();
                            HttpServletRequest request = attributes.getRequest();
                            Enumeration<String> headerNames = request.getHeaderNames();
                                          requestTemplate.header("xiah", "fxf217");
                     }
              };
       }


}
